package com.oos.zone.wechat.cp.controller;

import com.oos.zone.wechat.cp.service.WxCpEventNoticeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 功能描述：企业微信通知事件接收
 *
 * @author 拓金辉
 * @version 1.0
 * @date 2021-07-22 13:29:30
 * @see <a href="https://work.weixin.qq.com/api/doc/10514">接收消息与事件</>
 */
@RestController
@RequestMapping("/wechat/cp/event/notice/")
public class WxCpEventNoticeController {

    @Resource
    private WxCpEventNoticeService wxCpEventNoticeService;

    /**
     * 方法描述：微信通知事件
     *
     * @param corpId       路径参数，企业id
     * @param agentId      路径参数，应用id
     * @param msgSignature 从接收消息的URL中获取的msg_signature参数企业微信加密签名，msg_signature结合了企业填写的token、请求中的timestamp、nonce参数、加密的消息体
     * @param timestamp    从接收消息的URL中获取的timestamp参数
     * @param nonce        从接收消息的URL中获取的nonce参数
     * @param encryptedXml 从接收消息的URL中获取的整个post数据
     * @return java.lang.String
     * @author 拓金辉
     * @date 2021-07-30 14:37:07
     */
    @PostMapping(value = "/{corpId}/{agentId}")
    public String notice(@PathVariable(value = "corpId") String corpId,
                         @PathVariable(value = "agentId") String agentId,
                         @RequestParam(value = "msg_signature") String msgSignature,
                         @RequestParam(value = "timestamp") String timestamp,
                         @RequestParam(value = "nonce") String nonce,
                         @RequestBody String encryptedXml) {
        return wxCpEventNoticeService.notice(corpId, agentId, msgSignature, timestamp, nonce, encryptedXml);
    }

    /**
     * 方法描述：验证URL有效性
     *
     * @param corpId       路径参数，企业id
     * @param agentId      路径参数，应用id
     * @param msgSignature 从接收消息的URL中获取的msg_signature参数企业微信加密签名，msg_signature结合了企业填写的token、请求中的timestamp、nonce参数、加密的消息体
     * @param timestamp    从接收消息的URL中获取的timestamp参数
     * @param nonce        从接收消息的URL中获取的nonce参数
     * @param echoStr      从接收消息的URL中获取的echostr参数
     * @return java.lang.String
     * @author 拓金辉
     * @date 2021-07-30 14:37:07
     */
    @GetMapping(value = "/{corpId}/{agentId}")
    public String verify(@PathVariable(value = "corpId") String corpId,
                         @PathVariable(value = "agentId") String agentId,
                         @RequestParam(value = "msg_signature") String msgSignature,
                         @RequestParam(value = "timestamp") String timestamp,
                         @RequestParam(value = "nonce") String nonce,
                         @RequestParam(value = "echostr") String echoStr) {
        return wxCpEventNoticeService.verify(corpId, agentId, msgSignature, timestamp, nonce, echoStr);
    }

}
